﻿using oceanho.webapi.factory.httpAttr;
using oceanho.webapi.factory.httpCore;
using oceanho.webapi.factory.httpEntity;
using oceanho.webapi.factory.httpHandler;
using oceanho.webapi.factory.Tests.AppCode;
using oceanho.webapi.factory.Tests.Entity;
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace oceanho.webapi.factory.Tests.serviceHandlers
{
    /// <summary>
    /// 定义一个AppBase_ServeiceHandler,继承自http_service_handler,用来当作是本应用程序的httphandler服务父类
    /// </summary>
    public class AppBase_ServeiceHandler : http_service_handler
    {
        /// <summary>
        /// 重写 OnExcuting ，对请求合法性进行处理
        /// </summary>
        /// <param name="argument"></param>
        protected override void OnExcuting(RunHandlerEvent argument)
        {
            // base.OnExcuting(argument);
        }

        public void GetUserMessageList(string call, User user)
        {
            Jsonp(call, 123);
        }

        #region o(∩_∩)o —测试禁止调用的义务方法——

        [UnAllowCallMethod]
        public void Login(int id, string name, int age = 1500)
        {
            Html("Login -> id：" + id + " -> name：" + name + " -> age:" + age);
        }
        #endregion

        #region o(∩_∩)o —测试请求参数的解析功能——

        /// <summary>
        /// 测试 .jsp/.htm/.html/.shtml/.cshtml等后缀的路由调用
        /// </summary>
        /// <param name="user"></param>
        [Router("api/user/register.jsp")]
        // [CallRouter("api/user/register.htm")]
        // [CallRouter("api/user/register.html")]
        // [CallRouter("api/user/register.shtml")]
        // [CallRouter("api/user/register.cshtml")]
        public void Register(User user)
        {
            Html("Login -> id：" + user.id + " -> name：" + user.name + " -> age:" + user.age);
        }

        /// <summary>
        /// 测试单个参数的解析,需要form表单/queryString里面包含User的属性就可以解析了
        /// </summary>
        /// <param name="user"></param>
        [Router("api/user/login")]
        public void LoginTo(User user)
        {
            Html("Login -> id：" + user.id + " -> name：" + user.name + " -> age:" + user.age);
        }

        /// <summary>
        /// 测试单个参数的解析,需要form表单/queryString里面包含User的属性就可以解析了
        /// </summary>
        /// <param name="user"></param>
        [Router("api/user/testbinder")]
        public void CustomBinderTest([ParamBinder(typeof(AppDefaultBinder))]User user)
        {
            Html("Login -> id：" + user.id + " -> name：" + user.name + " -> age:" + user.age);
        }

        /// <summary>
        /// 测试数组类参数的解析,需要参数格式为users[0].id=xx这样的才可以正常解析
        /// </summary>
        /// <param name="users"></param>
        [Router("api/user/getArray")]
        public void LoginTo(User[] users)
        {
            if (users != null)
            {
                int i = 0;
                foreach (var user in users)
                {
                    Html("第" + (++i) + "个 -> id：" + user.id + " -> name：" + user.name + " -> age:" + user.age + "<hr/>");
                }
            }
        }

        /// <summary>
        /// 测试数组类参数的解析,需要参数格式为users[0].id=xx这样的才可以正常解析
        /// </summary>
        /// <param name="users"></param>
        [Router("api/user/getList")]
        public void LoginTo(List<User> users)
        {
            if (users != null)
            {
                int i = 0;
                foreach (var user in users)
                {
                    Html("第" + (++i) + "个 -> id：" + user.id + " -> name：" + user.name + " -> age:" + user.age + "<hr/>");
                }
            }
        }

        /// <summary>
        /// 测试数组类参数的解析,需要参数格式为ids[0]=xx这样的才可以正常解析
        /// </summary>
        /// <param name="users"></param>
        [Router("api/user/getListV2")]
        public void LoginTo(string[] ids)
        {
            if (ids != null)
            {
                int i = 0;
                foreach (var id in ids)
                {
                    Html("第" + (++i) + "个 -> id：" + id + "<hr/>");
                }
            }
        }

        /// <summary>
        /// 测试数组类参数的解析,需要参数格式为ids[0]=xx这样的才可以正常解析
        /// </summary>
        /// <param name="users"></param>
        [Router("api/user/getListV2.htm")]
        public void LoginTo(int[] ids)
        {
            if (ids != null)
            {
                int i = 0;
                foreach (var id in ids)
                {
                    Html("第" + (++i) + "个 -> id：" + id + "<hr/>");
                }
            }
        }

        /// <summary>
        /// 测试数组类参数的解析,需要参数格式为ids[0]=xx这样的才可以正常解析
        /// </summary>
        /// <param name="users"></param>
        [Router("api/user/getListV2.php")]
        public void LoginTo(List<int> ids)
        {
            if (ids != null)
            {
                int i = 0;
                foreach (var id in ids)
                {
                    Html("第" + (++i) + "个 -> id：" + id + "<hr/>");
                }
            }
        }
        #endregion

        #region o(∩_∩)o —测试请求参数的解析功能——

        /// <summary>
        /// 测试 复杂类型中包括复杂类型 的义务方法参数解析
        /// </summary>
        /// <param name="user"></param>
        [Router("api/user/GetStudentCourse")]
        // [CallRouter("api/user/register.htm")]
        // [CallRouter("api/user/register.html")]
        // [CallRouter("api/user/register.shtml")]
        // [CallRouter("api/user/register.cshtml")]
        public void GetStudentCourse(List<Student> students)
        {
            if (students != null)
            {
                int i = 0;
                StringBuilder sb = new StringBuilder();

                foreach (var stu in students)
                {
                    sb.AppendFormat("第" + (++i) + "个 -> stu.ID：" + stu.ID + " -> stu.Name：" + stu.Name + " -> stu.Age:" + stu.Age + "<br/>");
                    sb.AppendFormat("学生课程表如下：<br/>");
                    if (stu.Courses != null)
                    {
                        foreach (var course in stu.Courses)
                        {
                            sb.AppendFormat("{0} -> {1}", course.ID, course.Name + "<br/>");
                        }
                        sb.AppendFormat("<hr/>");                        
                    }
                    Html(sb.ToString());
                    sb.Clear();
                }
            }
        }
        #endregion
    }
}